package com.igd.action.user;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.util.WebUtils;
import org.apache.struts2.ServletActionContext;

import com.igd.action.BaseAction;
import com.igd.business.service.IUserService;
import com.igd.common.constant.RequestConstant;
import com.igd.common.constant.SessionConstant;
import com.igd.exceptions.IgdException;
import com.igd.pojo.vo.ExtData;
import com.igd.security.Resources;
import com.igd.security.User;

 /**
 * @version  1.0
 *
 * @author   QIAOYU
 *
 * @date     2013-6-25
 *
 * @description	 平台用户管理处理Action
 */

public class UserAction extends BaseAction {

	private static final long serialVersionUID = -6393320254730179167L;

	private Logger logger = Logger.getLogger(this.getClass());
	
	private IUserService userService;
	
	private User user;
	
	/** 用户信息请求Key */
	private final String USER_LIST_REQ_KEY = "USER_LIST_REQ_KEY";
	
	public String userModify() throws SQLException, IgdException{
		
		ExtData<User> extData = new ExtData<User>();
		
		if(null != this.getUser()){
			
			this.getUserService().updateUserInfo(this.getUser());

			extData.setMessage("user.modify.success");

		}else{
			
			extData.setMessage("user.modify.fail");

		}
		
		this.putExtData(extData);
		
		return this.JSON_PAGE;
	}
	
	/**
	 * 跳转到用户管理首页,并查询用户数据
	 * 
	 * @return
	 * 
	 * @throws SQLException
	 * 
	 * @throws IgdException
	 */
	public String userManager() throws SQLException, IgdException{
		
		this.getPage().setParam(this.pasrseReqParam());
		
		List<User> userList = this.getUserService().getAllUser(this.getPage());
		
		this.put(USER_LIST_REQ_KEY,userList);
		
		return SUCCESS;
	}
	
	/**
	 * 跳转到用户添加页面,其中可转发其他扩展数据
	 * 
	 * @return
	 */
	public String toUserAdd(){
		
		return SUCCESS;
	}
	
	/**
	 * 跳转到用户修改页面,其中可转发其他扩展数据
	 * 
	 * @return
	 * 
	 * @throws IgdException 
	 * 
	 * @throws SQLException 
	 */
	public String toUserModify() throws SQLException, IgdException{
		
		User user = this.getUserService().getUserInfoByUserId(this.getUser().getUserId());
		
		this.put(RequestConstant.REQ_USER_INFO,user);
		
		return SUCCESS;
	}
	
	/**
	 * 用户信息查看
	 * 
	 * @return
	 * 
	 * @throws SQLException
	 * 
	 * @throws IgdException
	 */
	public String toUserView() throws SQLException, IgdException{
		
		User user = this.getUserService().getUserInfoByUserId(this.getUser().getUserId());
		
		this.put(RequestConstant.REQ_USER_INFO,user);
		
		return SUCCESS;
		
	}
	
	/**
	 * 用户数据删除,此方法为数据库直接删除
	 * 
	 * @return
	 * 
	 * @throws SQLException
	 * 
	 * @throws IgdException
	 */
	public String userDel() throws SQLException, IgdException{
		
		ExtData<User> extData = new ExtData<User>();
		
		if(null != this.getUser()){
			
			this.getUserService().delUserInfo(this.getUser());

			extData.setMessage("user.del.success");

		}else{
			
			extData.setMessage("user.del.fail");

		}
		
		this.putExtData(extData);
		
		return this.JSON_PAGE;
	}
	
	/**
	 * 用户添加方法
	 * 
	 * @return
	 * 
	 * @throws SQLException
	 * 
	 * @throws IgdException
	 */
	public String userAdd() throws SQLException, IgdException{

		ExtData<User> extData = new ExtData<User>();
		
		if(null != this.getUser()){
			
			User user = this.getUserService().saveUserInfo(this.getUser());
			
			ArrayList<User> userList = new ArrayList<User>();

			userList.add(user);
			
			extData.setRspData(userList);
			
			extData.setMessage("user.add.success");
			
		}else{
			
			extData.setStatusCode(ExtData.FAIL);
			
			extData.setMessage("user.add.fail");
		}
		
		this.putExtData(extData);
		
		return 	this.JSON_PAGE;
	}

	/**
	 * 用户登录成功后跳转
	 * 
	 * @return
	 */
	public String loginSuccess(){
		
		User user = (User) SecurityUtils.getSubject().getSession().getAttribute(SessionConstant.LOGIN_USER_KEY);
		
		logger.info("login username is:"+user);
		
		if(null != user){

			if(null == user.getUserAllRes() || user.getUserAllRes().isEmpty()){
				
				ServletContext application = ServletActionContext.getServletContext();
				
				user.buildMenuTreeMap((List<Resources>) application.getAttribute(SessionConstant.ROOT_MENU_KEY));
				
			}
		}
		
		return SUCCESS;
	}
	
	/**
	 * 用户登出操作
	 * 
	 * @return
	 */
	public String logout(){
		
		Subject currentUser = SecurityUtils.getSubject();
		
		if(null != currentUser){
			
			WebUtils.getAndClearSavedRequest(this.getReuquest());
			
			currentUser.logout();

		}
		
		return SUCCESS;
	}
	
	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}
